package club.wx3609.images.service;

import club.wx3609.images.entity.Image;
import club.wx3609.images.interfaces.repository.ImageRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ImageService {

    @Resource
    private ImageRepository imageRepository;

    public Image addImage(Image image){
        return imageRepository.save(image);
    }

    public void deleteImage(Image image){
        imageRepository.delete(image);
    }

    public void deleteImage(Integer imageId){
        imageRepository.deleteById(imageId);
    }

    public List<Image> findAll(){return imageRepository.findAll();}

    public Image findById(Integer Id){
        return imageRepository.findByImageId(Id);
    }

    public List<Image> findImageUrlPath(String imageUrlPath){return imageRepository.findByImageUrlPath(imageUrlPath);}

    /**
     * 分页查询
     * @param pageNumber 当前页码
     * @param pageSize 每页所占的容量
     * @param sortTitle 给哪列排序
     */
    public Page<Image> findAllImage(Integer pageNumber,Integer pageSize,String sortTitle){
        Sort sort = new Sort(Sort.Direction.DESC, sortTitle);
        Pageable pageable = new PageRequest(pageNumber, pageSize, sort);
        Page<Image> page=imageRepository.findAll(pageable);
        return page;
    }
}
